package cn.core.dirs.service.impl;

import cn.core.dirs.ao.FileUploadAO;
import cn.core.dirs.eo.Files;
import cn.core.dirs.mapper.FilesMapper;
import cn.core.dirs.service.intf.IFileUploadService;
import cn.core.tool.exception.CoreException;
import cn.core.tool.util.CoreTool;
import cn.core.tool.util.Paging;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.Collection;
import java.util.LinkedHashSet;

/**
 * 文件上传 服务实现
 */
@Log4j2
@Primary
@Service
public class FileUploadServiceImpl implements IFileUploadService {

  @Value("${proconf.file-sv-type}")
  private String fileSvType;

  @Value("${proconf.local-file-path}")
  private String localFilePath;

  @Value("${proconf.file-path-prefix}")
  private String filePathPrefix;

  @Resource
  private FilesMapper filesMapper;

  @Override
  public Collection<String> fileUpload(FileUploadAO fileUploadAO) throws Exception {
    try {
      if (CoreTool.isNullStr(fileUploadAO.getFileModule())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      if (fileUploadAO.getFile() == null) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      if (fileUploadAO.getFile().length == 0) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      LinkedHashSet<String> r = new LinkedHashSet<>();
      LinkedHashSet<Files> saveFilesSet = new LinkedHashSet<>();
      if ("local".equals(fileSvType)) {
        String dateUrl = CoreTool.getCurrentFormatTime("yyyy/MM/dd");
        for (MultipartFile multipartFile : fileUploadAO.getFile()) {
          StringBuilder fileUrl = new StringBuilder();
          fileUrl.append(filePathPrefix)
              .append("/")
              .append(fileUploadAO.getFileModule())
              .append("/")
              .append(dateUrl)
              .append("/")
              .append(CoreTool.getUUID32())
              .append(multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf(".")));
          String path = localFilePath + fileUrl.toString();
          File f = new File(path);
          if (!f.exists()) {
            new File(path.substring(0, path.lastIndexOf("/"))).mkdirs();
          }
          multipartFile.transferTo(f);
          r.add(fileUrl.toString());
          saveFilesSet.add(new Files()
              .setId(CoreTool.getUUID32())
              .setFileModule(fileUploadAO.getFileModule())
              .setFileName(multipartFile.getOriginalFilename())
              .setFileUrl(fileUrl.toString())
              .setUploadTimes(CoreTool.getCurrentTimeStamp()));
        }
      } else if ("qiniu-kodo".equals(fileSvType)) {
      } else if ("aliyun-oss".equals(fileSvType)) {
      } else if ("tencent-cos".equals(fileSvType)) {
      } else {
      }
      if (CoreTool.isNotEmpty(saveFilesSet)) {
        filesMapper.saveBatch(new Paging<>(), saveFilesSet);
      }
      return r;
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
  }

}
